package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.AlterDemand;
import com.example.demo.entity.Demand;
import com.example.demo.entity.QueryInfo;
import com.example.demo.service.DemandService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/Demand")
@Api(value = "需求接口", tags = "需求相关的接口" )

public class DemandContorller {
    @Autowired
    DemandService demandService;

    //  更改需求状态，所需参数ID，状态
    @RequestMapping(value = "/updateDemand",method = RequestMethod.PUT)
    @ApiOperation(value = "更改需求状态", notes = "更改需求状态")
    public String updateDemand(Demand demand) {
        System.out.println(demand);
        int i = demandService.updateDemand(demand);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping(value = "/findDemand",method = RequestMethod.GET)
    @ApiOperation(value = "搜索需求并返回条数", notes = "搜索需求并返回条数")
    public String getAlterDemandList(QueryInfo queryInfo){
        int numbers = demandService.getDemandCounts(queryInfo.getBgProject(),queryInfo.getState());// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Demand> demands = demandService.getDemand(queryInfo.getBgProject(),queryInfo.getState(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("number",numbers);
        res.put("data",demands);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println(users_json);
        return users_json;
    }

    //  所需参数page size
    @RequestMapping(value = "/showAllDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示所有需求", notes = "显示所有需求")
    public List<Demand> pageQueryAll(int page, int size){
        return demandService.pageQueryAll(page,size);
    }

    //  所需参数page size
    @RequestMapping(value = "/showUnbeginDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示未开始需求", notes = "显示未开始需求")
    public List<Demand> pageQueryUnbegin(int page, int size){
        return demandService.pageQueryUnbegin(page,size);
    }

    //  所需参数page size
    @RequestMapping(value = "/showBeginDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示开始需求", notes = "显示开始需求")
    public List<Demand> pageQueryBegin(int page, int size){
        return demandService.pageQueryBegin(page,size);
    }

    //  所需参数page size
    @RequestMapping(value = "/showQuitDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示取消需求", notes = "显示取消需求")
    public List<Demand> pageQueryQuit(int page, int size){
        return demandService.pageQueryQuit(page,size);
    }

    //  所需参数page size
    @RequestMapping(value = "/showFinishDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示完成需求", notes = "显示完成需求")
    public List<Demand> pageQueryFinish(int page, int size){
        return demandService.pageQueryFinish(page,size);
    }

    //  所需参数page size
    @RequestMapping(value = "/showCorrespondDemand",method = RequestMethod.GET)
    @ApiOperation(value = "显示指定需求", notes = "显示指定需求")
    public List<Demand> pageQueryCrosspond(int page, int size, String bgProject){
        return demandService.pageQueryCrosspond(page,size,bgProject);
    }

}
